

/**
 * global ajax error setup
 */
$(document).ready(function () {
	$("body").bind("ajaxError", function(event, request, settings){
		if(request.responseText)
			alert(request.responseText);
		else
			alert("Error requesting page: " + settings.url);
	});	
	
	$('#ui_elements').on('click', function () {
		save_to_cookie('ui_elements')
		save_to_cookie('forms', false)
	});
	$('#forms').on('click', function () {
		save_to_cookie('forms')
		save_to_cookie('ui_elements', false)
		
	});
	
	check_cookie('ui_elements');
	check_cookie('forms');
	
	return_messages();
	online_user();
	
	var refreshId = setInterval( function()
    {
		return_mail();
		return_messages();
		online_user();
    }, 5*60*1000);
	
	$('.dialogs,.comments').slimScroll({
			        height: '300px'
	});
	
	
	
	var data = [
				{ label: "social networks",  data: 38.7, color: "#68BC31"},
				{ label: "search engines",  data: 24.5, color: "#2091CF"},
				{ label: "ad campaings",  data: 8.2, color: "#AF4E96"},
				{ label: "direct traffic",  data: 18.6, color: "#DA5430"},
				{ label: "other",  data: 10, color: "#FEE074"}
			  ];
			
			  var placeholder = $('#piechart-placeholder').css({'width':'90%' , 'min-height':'150px'});
			  $.plot(placeholder, data, {
				
				series: {
			        pie: {
			            show: true,
						tilt:0.8,
						highlight: {
							opacity: 0.25
						},
						stroke: {
							color: '#fff',
							width: 2
						},
						startAngle: 2
						
			        }
			    },
			    legend: {
			        show: true,
					position: "ne", 
				    labelBoxBorderColor: null,
					margin:[-30,15]
			    }
				,
				grid: {
					hoverable: true,
					clickable: true
				},
				tooltip: true, //activate tooltip
				tooltipOpts: {
					content: "%s : %y.1",
					shifts: {
						x: -30,
						y: -50
					}
				}
				
			 });
			
			 
			  var $tooltip = $("<div class='tooltip top in' style='display:none;'><div class='tooltip-inner'></div></div>").appendTo('body');
			  placeholder.data('tooltip', $tooltip);
			  var previousPoint = null;
			
			  placeholder.on('plothover', function (event, pos, item) {
				if(item) {
					if (previousPoint != item.seriesIndex) {
						previousPoint = item.seriesIndex;
						var tip = item.series['label'] + " : " + item.series['percent']+'%';
						$(this).data('tooltip').show().children(0).text(tip);
					}
					$(this).data('tooltip').css({top:pos.pageY + 10, left:pos.pageX + 10});
				} else {
					$(this).data('tooltip').hide();
					previousPoint = null;
				}
				
			 });
			 
			 
			 
			 
	
});


/**
 * Does sum with ajax
 */
function sum_ajax(){
	$('#sum_result').load('calculateajax', {num1: $('#num1').val(), num2: $('#num2').val()});
}

function save_to_cookie(el_id, visible=true) {
	var cookieValue = $.cookie(el_id);
	if (cookieValue == 'visible') {
		$.cookie(el_id , 'hidden', { path: '/' });
	} else if (visible){
		$.cookie(el_id , 'visible', { path: '/' });
	}
}

function check_cookie(el_id) {
	var cookieValue = $.cookie(el_id);
	if (cookieValue == 'visible') {
		$('#' + el_id + '_li').addClass( "open" );
		$('#' + el_id + '_submenu').css("display", "block");
	} else {
		$('#' + el_id + '_li').removeClass("open");
		$('#' + el_id + '_submenu').css("display", "none");
	}
}

function return_mail() {
	var basedir_web = $('#basedir_web').val();
	$.getJSON( basedir_web + "/default/countmail", function( data ) {
		$('.new_mails').html(data[0].unread);
		$.each(data[1], function(i, field){
			$('#last_3_mails').append('<li>' +
											'<a href="#">' +
												'<span class="msg-body">' +
													'<span class="msg-title">' +
														'<span class="blue">' + field.mail_from + ': </span>' +
														  field.mail_subject + 
													'</span>' +

													'<span class="msg-time">' +
														'<i class="icon-time"></i> ' +
														'<span>a moment ago</span>' +
													'</span>' +
												'</span>' +
											'</a>'+
										'</li>');
		});
		$('#last_3_mails').append('<li>'+
										'<a href="#">'+
											'See all messages'+
											'<i class="icon-arrow-right"></i>'+
										'</a>'+
									'</li>');
	});
	$('#last_3_mails').html('<li class="nav-header">'+
								'<i class="icon-envelope"></i>'+
								'<span class="new_mails">0</span> Unread e-mails'+
							'</li>');
}

function return_messages() {
	var basedir_web = $('#basedir_web').val();
	$.getJSON( basedir_web + "/ajax/countmessages", function( data ) {
		$('#new_messages').html(data['message'].unread);
		$('#new_to_do').html(data['to_do'].unread);
		$('#new_task').html(data['task'].unread);
		$('#new_important').html(data['important'].unread);
		$('.new_all').html(data['sum'].unread);
	});
}

function online_user() {
	var basedir_web = $('#basedir_web').val();
	var count = 0;
	$('#online_user').html('<li class="nav-header">'+
							'<i class="icon-comments-alt"></i>'+
							'<span class="online_user_all">0</span> Korisnika online'+
						'</li>');
	$.getJSON( basedir_web + "/ajax/onlineuser", function( data ) {
		$.each(data['online_user'], function(i, field){
			count++;
			$('#online_user').append('<li>'+
										'<a href="#">'+
											'<img class="msg-photo" alt="Alex\'s Avatar" src="'+ basedir_web +'/lib/themes/images/'+ field.username +'.png">'+
											'<span class="msg-body">'+
												'<span class="msg-title">'+
													field.full_name +
												'</span>'+
												'<span class="msg-time">'+
													'<i class="icon-time"></i> '+
													'<span>'+ field.last_activity +'</span>'+
												'</span>'+
											'</span>'+
										'</a>'+
									'</li>');
		});
		$('#online_user').append('<li>'+
										'<a href="#">'+
											'See all users'+
											'<i class="icon-arrow-right"></i>'+
										'</a>'+
									'</li>');
		
		$('.online_user_all').html(data['count'].online_user_all);
	});
	
}



